package com.kobe.game_20;

import java.util.ArrayList;
import java.util.List;

import com.kobe.util.MathUtil;

/**
 * 
 * The prime factors of 13195 are 5, 7, 13 and 29.
 * 
 * What is the largest prime factor of the number 600851475143 ?
 * 
 * 
 * the result is: 6857
 */
public class Game_3 {

    public static void main(String[] args) {
        long theNumber = 600851475143L;
        List<Integer> factors = new ArrayList<Integer>();

        // to find out all the factors.
        for (int i = 2;; i++) {
            while (theNumber % i == 0) {
                theNumber = theNumber / i;
                factors.add(i);
            }
            if (theNumber == 1) {
                break;
            }
        }

        // find the max prime factor
        for (int index = factors.size() - 1; index >= 0; index--) {
            if (MathUtil.isPrime(factors.get(index))) {
                System.out.println(factors.get(index));
                break;
            }
        }
    }
}
